﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class FileHandler_FileHandler : System.Web.UI.Page
{
    private bool _hasProcessDetails = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
            BindDetails();
        }

        Repeater1.Visible = _hasProcessDetails;

    }

    private void BindData()
    {
        if (Request.QueryString["key"] != null)
        {
            var orderid = Request.QueryString["key"];

            if (!string.IsNullOrEmpty(orderid))
            {
                if (orderid.Length == 10)
                {
                    Label1.Text = orderid;
                    OrderID.Value = orderid;

                    var company = new CompanyBLL().GetCompanyInfoByOrderID(orderid);

                    if (company != null)
                    {
                        CompanyID.Text = company.CompanyID;
                        EnglishName.Text = company.EnglishName;
                        ChineseName.Text = company.ChineseName;
                        SerialNumber.Value = company.SerialNumber;
                        Label2.Text = company.RegisterDate.ToString("yyyy-MM-dd");
                    }

                    var order = new OrderBLL().GetOrderInfoByOrderID(orderid);
                    if (order != null)
                    {
                        OrderRemark.Text = order.Remark;
                    }
                }
            }
        }
    }

    private void BindDetails()
    {
        if (Request.QueryString["key"] != null)
        {
            var orderid = Request.QueryString["key"];

            if (!string.IsNullOrEmpty(orderid))
            {
                if (orderid.Length == 10)
                {
                    var details = new FileProcessDetailBLL().GetDetailsByOrderID(orderid);

                    if(details != null && details.Count > 0)
                    {
                        Repeater1.DataSource = details;
                        Repeater1.DataBind();
                        _hasProcessDetails = true;
                    }
                    
                }
            }
        }
    }

    /// <summary>
    /// 处理成功
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        // 更新公司信息
        UpdateCompanyInfo();

        // 写入订单轨迹
        var empolyeeID = new EmpolyeeBLL().GetEmpolyeeInfoByLoginName(User.Identity.Name).EmpolyeeID;
        new OrderTrackBLL().AddTrack(OrderID.Value, empolyeeID, Koonsoft.HYCRM.Model.OrderStatus.FileProcess, 1, Remark.Text.Trim());

        // 通知订单状态改变
        new OrderBLL().ChangeOrderStatusByOrderID(OrderID.Value, Koonsoft.HYCRM.Model.OrderStatus.CenterDeliver);

        // 跳回来源页面
        JumpBack();
    }

    /// <summary>
    /// 处理失败
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        // 更新公司信息
        UpdateCompanyInfo();

        // 写入订单轨迹
        var empolyeeID = new EmpolyeeBLL().GetEmpolyeeInfoByLoginName(User.Identity.Name).EmpolyeeID;
        new OrderTrackBLL().AddTrack(OrderID.Value, empolyeeID, Koonsoft.HYCRM.Model.OrderStatus.FileProcess, 2, Remark.Text.Trim());

        // 通知订单状态改变
        new OrderBLL().ChangeOrderStatusByOrderID(OrderID.Value, Koonsoft.HYCRM.Model.OrderStatus.ProcessExcpetion);

        // 跳回来源页面
        JumpBack();
    }

    /// <summary>
    /// 开始文件受理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button3_Click(object sender, EventArgs e)
    {
        // 通知订单状态改变
        //new OrderBLL().ChangeOrderStatusByOrderID(OrderID.Value, 1);
    }

    /// <summary>
    /// 更新公司信息
    /// </summary>
    private void UpdateCompanyInfo()
    {
        if (!string.IsNullOrEmpty(SerialNumber.Value))
        {
            var companyID = CompanyID.Text.Trim();
            var englishName = EnglishName.Text.Trim();
            var chineseName = ChineseName.Text.Trim();
            var serialNumber = SerialNumber.Value.Trim();
            string regDate = Label2.Text;

            new CompanyBLL().FileHandleByCompanySerialNumber(companyID, englishName, chineseName, serialNumber, regDate);
        }
    }

    /// <summary>
    /// 跳回来源页面
    /// </summary>
    private void JumpBack()
    {
        if (Request.QueryString["url"] != null)
        {
            switch (Request.QueryString["url"])
            {
                case "editor":
                    Response.Redirect("HongKongFileEditor.aspx");
                    break;

                case "helper":
                    Response.Redirect("HongKongFileHelper.aspx");
                    break;

                case "handler":
                    Response.Redirect("OtherRegionFileHandler.aspx");
                    break;
            }
        }
    }

}